<template>
    <view>
    <u-alert title="此投诉为本小程序自有投诉渠道，非微信官方投诉渠道" type = "warning"></u-alert>
    <view class="content p_10">
        <view class="content_item p_10 m_b_10">
            <u--textarea v-model="params.nr" border="none" placeholder="请填写反馈内容"></u--textarea>
            <u-upload :fileList="fileList1" @afterRead="afterRead" @delete="deletePic" name="1" multiple></u-upload>
        </view>
        <view class="dis_f_c_c m_t_50 ">
            <u-button type="primary"  @click="handleSend" text="提交"></u-button>
        </view>
        <s-auth-modal/>
    </view>
    </view>
</template>
<script>
import {
    addComplaintApi,
} from "@/sheep/api/index";
import {
    baseUrl
} from "@/sheep/request/baseUrl";

export default {
    name: "index",
    data() {
        return {
            fileList1: [],
            params: {
                nr: '',
            },
        }
    },
    onShow() {

    },
    onLoad(options) {
        this.params.createBy = options?.userId

    },
    methods: {
        handleSend() {
            if (!this.params.nr) {
                uni.showToast({
                    title:'请填写反馈内容',
                    icon: 'none'
                })
                return
            }
            if (Array.isArray(this.fileList1)) {
                if (this.fileList1.length > 0) {
                    this.fileList1.forEach((item, index) => {
                        if (index === 0) {
                            this.params.urlId = item.ossId;
                        } else {
                            this.params.urlId = this.params.urlId + "," + item.ossId;
                        }
                    });
                } else {
                    this.params.urlId = "";
                }
            }
            uni.showLoading({});
            addComplaintApi(this.params).then(res => {
                uni.hideLoading()
                uni.showToast({
                    title: res.msg,
                    icon: 'none'
                })
                if (res.code == 200) {
                    setTimeout(() => {
                        //关闭当前页面 返回上一页面
                        uni.navigateBack({
                            delta: 1
                        });
                    }, 1000);
                }
            })
        },
        // 上传图片
        async afterRead(event) {
            let lists = [].concat(event.file);
            console.log("lists", lists);
            let fileListLen = this[`fileList${event.name}`].length;
            lists.map((item, index) => {
                this[`fileList${event.name}`].push({
                    ...item,
                    status: "uploading",
                    message: "上传中",
                });
            });
            for (let i = 0; i < lists.length; i++) {
                const result = await this.uploadFilePromise(lists[i].url, lists, event);
                console.log("result", result);
                let item = this[`fileList${event.name}`][fileListLen];
                this[`fileList${event.name}`].splice(
                    fileListLen,
                    1,
                    Object.assign(item, {
                        status: "success",
                        message: "",
                        url: result.url,
                        name: result.fileName,
                        ossId: result.ossId
                    })
                );
                fileListLen++;
            }
        },
        uploadFilePromise(url, lists, event) {
            let token = uni.getStorageSync('token')
            let headerToken = "Bearer " + token
            return new Promise((resolve, reject) => {
                let a = uni.uploadFile({
                    url: baseUrl + "/resource/oss/upload",
                    filePath: url,
                    name: "file",
                    header: {
                        Authorization: headerToken,
                        clientId: "be7052a7e4f802c20df10a8d131adb12"
                    },
                    formData: {
                        user: "test",
                    },
                    success: (res) => {
                        let data = JSON.parse(res.data);
                        console.log("data", data);
                        if (data.code == 200) {
                            resolve(data.data);
                        } else {
                            if (lists.length > 0) {
                                lists.map((item, index) => {
                                    this[`fileList${event.name}`].splice(index, 1)
                                });
                            }
                            uni.showToast({
                                title: data.msg,
                                icon: "none",
                            });
                        }
                    },
                });
            });
        },
        // 删除图片
        deletePic(event) {
            this[`fileList${event.name}`].splice(event.index, 1);
        },
    }
}
</script>

<style scoped lang="scss">
.content {
    background-color: #f5f5f5;
    height: 100vh;

    .content_item {
        background: #ffffff;
        border-radius: 20rpx;
    }
}

.release {
    width: 100rpx;
    height: 70rpx;
    font-size: 28rpx;
    border-radius: 10rpx;
    background-color: #3c9cff;
    color: #ffffff;
}

</style>
